package org.jeecg.modules.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.grand.entity.GrandBar;
import org.jeecg.modules.grand.mapper.GrandBarMapper;
import org.jeecg.modules.report.entity.dal.ParamEntity;
import org.jeecg.modules.report.entity.dto.ClassExamContrastDto;
import org.jeecg.modules.report.entity.dto.CourseScoreDto;
import org.jeecg.modules.report.entity.dto.GrandFormDto;
import org.jeecg.modules.report.entity.dto.GrandFormTempDto;
import org.jeecg.modules.report.entity.vo.GrandFromVo;
import org.jeecg.modules.report.mapper.GrandFormMapper;
import org.jeecg.modules.report.service.IGrandFormService;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: jeecg-boot
 * @description: 成绩单
 * @author: vic
 * @create: 2021-05-09 15:46
 */
@Service
@Slf4j
public class GrandFormServiceImpl implements IGrandFormService {
    @Autowired
    private GrandFormMapper grandFormMapper;

    @Autowired
    private GrandBarMapper grandBarMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Override
    public GrandFromVo grandVo(ParamEntity paramEntity) {
        GrandFromVo grandFromVo=new GrandFromVo();
        //获取列表
        List<GrandFormDto> list=grandFormMapper.grandList(paramEntity);
        grandFromVo.setGrandList(list);
        //求最大值
        //求最小值
        //平均值
        GrandFormTempDto grandFormTempDto=grandFormMapper.grandTemp(paramEntity);
        if(grandFormTempDto!=null){
            if(grandFormTempDto.getAvgScore()!=null){
                grandFromVo.setAvgScore(grandFormTempDto.getAvgScore());
            }else{
                grandFromVo.setAvgScore(new BigDecimal(0.00));
            }
            if(grandFormTempDto.getMaxScore()!=null){
                grandFromVo.setMinScore(grandFormTempDto.getMinScore());
            }else {
                grandFromVo.setMinScore(new BigDecimal(0.0));
            }
            if(grandFormTempDto.getMinScore()!=null){
                grandFromVo.setMaxScore(grandFormTempDto.getMaxScore());
            }else{
                grandFromVo.setMaxScore(new BigDecimal(0.00));
            }
        }



        return grandFromVo;
    }

    @Override
    public List<ClassExamContrastDto> getClassExamContrast(ParamEntity paramEntity) {

        setParmart(paramEntity);

        return grandFormMapper.getClassScore(paramEntity);
    }

    @Override
    public List<ClassExamContrastDto> getClassExamCourseStu(ParamEntity paramEntity) {
        setParmart(paramEntity);
        return grandFormMapper.getClassCourseStu(paramEntity);
    }

    @Override
    public List<CourseScoreDto> getCourseScore(ParamEntity paramEntity) {

        if(StringUtils.isEmpty(paramEntity.getWorkNo())){
            //获取当前用户
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            paramEntity.setUserName(sysUser.getRealname());
        }


        return grandFormMapper.courseScore(paramEntity);
    }

    /**
     * 设置区间参数
     * @param paramEntity
     */
    private void setParmart(ParamEntity paramEntity){
        //设置查询区间
        LambdaQueryWrapper<GrandBar> queryWrapper=new LambdaQueryWrapper<>();
        List<GrandBar> grandBars=grandBarMapper.selectList(queryWrapper);
        log.info("获取的区间："+grandBars.toString());
        log.info("返回的int值："+grandBars.get(0).getStartScore().intValue());
        paramEntity.setAStartCount(grandBars.get(0).getStartScore().intValue());
        paramEntity.setAEndCount(grandBars.get(0).getEndScore().intValue());
        paramEntity.setBStartCount(grandBars.get(1).getStartScore().intValue());
        paramEntity.setBEndCount(grandBars.get(1).getEndScore().intValue());
        paramEntity.setCStartCount(grandBars.get(2).getStartScore().intValue());
        paramEntity.setCEndCount(grandBars.get(2).getEndScore().intValue());
        paramEntity.setDStartCount(grandBars.get(3).getStartScore().intValue());
        paramEntity.setDEndCount(grandBars.get(3).getEndScore().intValue());
    }
}
